#include "stdafx.h"
#include "WMPUtils.h"
#include "..\WMP\Common.h"

#pragma unmanaged

struct FindClassStylesData {
	LPCTSTR className;
	DWORD styles;
	HWND hApp;
};

BOOL CALLBACK FindClassStylesWindow(HWND hWnd, LPARAM lParam) {
	CreateTextBuffer(classBuffer);

	FindClassStylesData* data = (FindClassStylesData*)lParam;
	GetClassName(hWnd, classBuffer, classBufferLength);
	if (lstrcmpi(classBuffer, data->className) != 0)
		return true;
	if (data->styles != 0 && (data->styles & GetWindowLong(hWnd, GWL_EXSTYLE)) == 0)
		return true;
	DWORD processId;
	GetWindowThreadProcessId(hWnd, &processId);
	if (GetCurrentProcessId() != processId)
		return true;
	data->hApp = hWnd;
	return false;
};

HWND WINAPI FindApplicationWindow(LPCTSTR className, DWORD styles) {
	FindClassStylesData data = {className, styles, NULL};
	EnumWindows(FindClassStylesWindow, (LPARAM)&data);
	return data.hApp;
}

HWND FindWMPLibraryWindow(HWND parent) {
	HWND hWnd;
	hWnd = FindWindowEx(parent, NULL, _T("WMPAppHost"), NULL);
	if (!hWnd) return NULL;
	hWnd = FindWindowEx(hWnd, NULL, _T("WMP Skin Host"), NULL);
	if (!hWnd) return NULL;
	hWnd = FindWindowEx(hWnd, NULL, NULL, _T("LibraryContainer"));
	return hWnd;
}

HWND FindWMPLibraryListWindow(HWND parent) {
	HWND hWnd;
	hWnd = FindWMPLibraryWindow(parent);
	if (!hWnd) return NULL;
	hWnd = FindWindowEx(hWnd, NULL, _T("SysListView32"), _T("PrimaryListView"));
	return hWnd;
}

HWND FindWMPLibraryPlaylistWindow(HWND parent) {
	HWND hWnd;
	hWnd = FindWMPLibraryWindow(parent);
	if (!hWnd) return NULL;
	hWnd = FindWindowEx(hWnd, NULL, _T("SysListView32"), _T("BasketListView"));
	return hWnd;
}

bool IsBarVisible(HWND parent) {
	HWND hWnd = FindWindowEx(parent, NULL, _T("ReBarWindow32"), NULL);
	if (hWnd)
		return !!IsWindowVisible(hWnd);
	return FALSE;
}

bool IsEditBox(HWND hWnd) {
	CreateTextBuffer(classBuffer);

	RealGetWindowClass(hWnd, classBuffer, classBufferLength);
	return lstrcmpi(classBuffer, _T("Edit")) == 0 || lstrcmpi(classBuffer, _T("Atl:Edit")) == 0 ||
		lstrcmpi(classBuffer, _T("Internet Explorer_Server")) == 0;
}
